$(document).ready(function() {
	dbConfig.init();
    dbConfig.getsourcelist();
});
var _mmg = null;
var editIndex = undefined;
var editRow=undefined;
var endEditing=undefined;
var dbConfig = function(){
    return{
        init: function () {
            _mmg = $('#dg').datagrid({
                url: '/dbConfigPage',
                params: $("#dbConfigForm").serialize(),
                method: 'post',
                rownumbers: true,
                fitColumns: true,
                striped: true,
                nowrap: true,
                pageList: [25, 30, 35],
                idField: 'id', //主键
                onClickRow: 'onClickRow',
                columns: [
                    [
                        {field: 'ck', checkbox: true},
                        {field: 'id', title: 'id', hidden: true},
                        {field: 'plat_form', title: '平台', width: 50},
                        {field: 'scrapy_name', title: '爬虫名称', width: 50},

                        {field: 'star_time', title: '爬取开始时间', width: 40, editor: {
                                type: 'datebox', options: {
                                }
                            }
                            },
                        {field: 'repetition_period', title: '重复周期（仅一天）', width: 40, editor: {
                                type: 'combobox', options: {
                                url: '/get_periodlist',
                                valueField: 'id',
                                textField: 'TEXT',
                                method: 'POST'
                                }
                            }
                            },
                         {
                             field: 'see_db',
                             title: '数据节点查看/设置',
                             width: 65,
                             type: 'String',
                             formatter: function (val, item, rowIndex) {
                                     return '<span><a href="#" onclick="dbConfig.see(\''+ item.id +'\')"><span style="color:blue;">查看</span></a></span>';
                             }
                             },
                        {field: 'db_capacity', title: '数据库容量', width: 50},
                        {
                             field: 'operation',
                             title: '操作',
                             width: 50,
                             type: 'String',
                             formatter: function (val, item, rowIndex) {
                                       // _mmg.datagrid("endEdit", editIndex);
                                      // return '<span><a href="#" onclick="dbConfig.save(\'' + item.id + ',' + item.star_time + ',' + item.repetition_period + '\')"><span style="color:blue;">保存</span></a></span>';
                                      return '<span><a href="#" onclick="dbConfig.scrapy()"><span style="color:blue;">保存</span></a></span>';
                             }
                             },
                    ]
                ],
                onClickRow: function (rowIndex, rowData) {
                          _mmg.datagrid('cancelEdit', editIndex);
                          _mmg.datagrid("beginEdit", rowIndex);
                          editIndex = rowIndex;

                    },
                // onBeforeEdit: function (rowIndex, rowData) {
                //          // console.info(rowData)
                //     },
                // onAfterEdit: function (rowIndex, rowData,changes) {
                //          // console.info(rowData)
                //
                //     },



            }).datagrid("clientPaging");
        },
        
           //初始化网站下拉框
        getsourcelist: function () {
            $('#source_info').combobox(
                {
                    url: '/getsourcelist',
                    valueField: 'id',
                    textField: 'text',
                    method: 'POST'
                }
            )
        },
        //编辑商品
        endEditing: function () {
            //开启编辑
            _mmg.datagrid("beginEdit", editIndex);

        },
          //查看节点设置
        see: function (id) {
            var  link='/dbDetail'
            window.location.href = link;
            
        },
          //保存设置
        scrapy: function () {
              _mmg.datagrid('endEdit', editIndex);
              // _mmg.datagrid('acceptChanges');
              var updateRows = _mmg.datagrid('getChanges');
              // var id= updateRows[0]['id']
              // var star_time= updateRows[0]['star_time']
              // var repetition_period= updateRows[0]['repetition_period']
              var scrapy_name;
              var plat_form;
              scrapy_name=updateRows[0]['scrapy_name']
              plat_form=updateRows[0]['plat_form']
              // var chackedItem = $('#dg').datagrid('getChecked');
              // scrapy_name=chackedItem[0]['scrapy_name']
              // alert(scrapy_name)
              // console.info(scrapy_name)
              $.messager.confirm('确认', '您确定要爬取该网站信息？', function (r) {
                if (r) {
                     $.ajax({
                            type: 'POST',
                            url: '/star_scrapy?scrapy_name=' + scrapy_name,
                            // url: '/star_scrapy?' + scrapy_name,
                            // data: 'id=' + integral_id,
                            success: function (result) {
                                if (result == '1') {
                                    alert("爬取信息成功")
                                    dbConfig.load();
                                    // return;
                                } else {
                                    alert('爬取信息失败！');
                                    return;
                                }
                            }
                        })
                     
                }
            });
            

        },

       //  onClickRow: function (index) {
       //     if(dbConfig.endEditing()){
       //   $('#dg').datagrid('selectRow',index).datagrid('beginedit',index)//其中beginEdit方法为datagrid的方法，具体可以参看api
       //   editIndex = index;//给editIndex对象赋值，index为当前行的索引
       //   }else {
       //      $('#dg').datagrid('selectRow',editIndex);
       //   }
       //  },
       // endEditing: function () {
       //  if(editIndex == undefined) {return true;}//如果为undefined的话，为真，说明可以编辑
       //   if($('#dg').datagrid('validateRow',editIndex)){
       //       ('#dg').datagrid('endEdit',editIndex);//当前行编辑事件取消
       //       editIndex = undefined;
       //       return true;//重置编辑行索引对象，返回真，允许编辑
       //      }
       //   else{return false;}//否则，为假，返回假，不允许编辑
       //  },

        //查询
        load: function () {
            $('#dg').datagrid('load',{
            });
        },
        //保存商品记录
        accept: function () {
            _mmg.datagrid('endEdit', editIndex);
            _mmg.datagrid('acceptChanges');

        },
         //打开弹出窗
        openPopu: function () {
            var xhrurl='http://115.159.79.198/mobileMember!login.do'
             $.ajax({
                            type :"post",
                            url :xhrurl,
                            dataType : "jsonP",
                            data: {username:'15750798687',password:'123456'},
                            success: function (result) {
                                console.info(result)
                            }
                        }
             )
           $('#w').window('open');
        },
         //关闭_弹出框
        closePopu: function () {
            $('#w').window('close', true);

        },
           //关闭_弹出框
        save: function () {
            var params = $("#platManage").serialize();
             $.ajax({
                    type: 'POST',
                    url: '/add_platForm',
                    dataType: 'json',
                    data: params,
                    success: function (result) {
                        if (result == '1') {
                            alert('添加平台信息成功！');
                            $('#platManage').form('clear');
                            dbConfig.clearForm('platManage');
                              dbConfig.closePopu();
                        } else {
                            alert('添加平台信息失败！');
                            dbConfig.closePopu();
                            return;
                        }
                    }
                })

        },
         //删除功能
       del: function () {
            var chackedItem = $('#dg').datagrid('getChecked');
            var plat_id_ids = [];
            var ids;
            if (chackedItem.length == '0') {
                alert("请至少选择一条记录！");
                return;
            }
            $.messager.confirm('确认', '您确定要删除当前选中的记录吗？', function (r) {
                if (r) {
                    if (chackedItem.length == 1) {
                        //删除单条记录
                        var chackedItem1 = $('#dg').datagrid('getChecked');
                        var plat_id;
                        $.each(chackedItem1, function (index, item) {
                            plat_id = item.id;
                        });
                        $.ajax({
                            type: 'POST',
                            url: '/del_plat_id_by_id',
                            data: 'id=' + plat_id,
                            success: function (result) {
                                if (result == '1') {
                                    alert("删除成功")
                                    dbConfig.load();
                                    return;
                                } else {
                                    alert('删除失败！');
                                    return;
                                }
                            }
                        })
                    } else {
                        //删除多条记录
                        $.each(chackedItem, function (index, item) {
                            if (plat_id_ids == "") {
                                plat_id_ids = "" + item.id + "";
                            } else {
                                plat_id_ids = "" + item.id + "" + "," + plat_id_ids;
                                ids = plat_id_ids.split(',');
                            }
                        });
                        $.ajax({
                            type: 'POST',
                            url: '/del_plat_id_by_ids',
                            data: 'id=' + JSON.stringify(ids),
                            success: function (result) {
                                if (result == '1') {
                                    alert('删除成功！');
                                    dbConfig.load();
                                    return;
                                } else {
                                    alert('删除失败！');
                                    return;
                                }
                            }
                        })
                    }
                }
            });
        },

       //清空表单
        clearForm: function (formID, unload) {


            // $("#" + formID).each(function () {
            //     $("#jd_name").val('')
            //     $("#first_price").val('')
            //     $("#last_price").val('')
            //     $("#firsttime").datetimebox('clear');
            //     $("#lasttime").datetimebox('clear');
            //     $('#source_info').combobox('clear');
            //     // iterate the elements within the form
            //     $(':input', this).each(function () {
            //         var type = this.type, tag = this.tagName.toLowerCase();
            //         if (type == 'text' || type == 'password' || tag == 'textarea' || type == "file")
            //             this.value = '';
            //         else if (type == 'checkbox' || type == 'radio')
            //             this.checked = false;
            //         else if (tag == 'select')
            //             $('#source_info').combobox('clear');
            //     });
            //     $('#dg').datagrid('load', {});
            // });
        }

    }

}
();

